<?php
	include '../init.php';

	//判断是否第一次打开本页面分页
	if (isset($_GET['page'])&&$_GET['page']>0) {
		$page=$_GET['page'];
	}else{
		$page=1;
	}



	//查找数据库
	$sql = "SELECT count(*) FROM article";
	if (isset($_GET['cid'])&&$_GET['cid']>0) {
		$sql .= " LEFT JOIN article_to_category on article.id=article_to_category.article_id WHERE category_id={$_GET['cid']}";
	}
	$count = $mysql->getOne($sql);
	$limit = 4;
	$p = new MyPage($count,$limit,$page);
	$pageHtml = $p->render();



	//通过分类查看文章的数据数据
	$sql = "SELECT article.* FROM article ";
	if (isset($_GET['cid'])&&$_GET['cid']>0) {
		$sql .= " LEFT JOIN article_to_category on article.id=article_to_category.article_id WHERE category_id={$_GET['cid']}";
	}
	$start = ($page-1)*$limit;
	$sql .= " limit {$start},{$limit}";
	$result = $mysql -> getAll($sql); 


	//文章搜索
	//文章页面进行搜索
	if (!empty($_GET['find'])&&!empty($_GET['seek'])) {

		$sql = "SELECT count(*) FROM article WHERE title LIKE '%{$_GET['seek']}%' or author LIKE '%{$_GET['seek']}%'";
		$count = $mysql->getOne($sql);
		$limit = 5;
		$p = new MyPage($count,$limit,$page);
		$pageHtml = $p->render();//搜索后的分页按钮

		// $seek=$_GET['seek'];
		$sql = "SELECT article.* FROM article WHERE title LIKE '%{$_GET['seek']}%' or author LIKE '%{$_GET['seek']}%'";
		$start = ($page-1)*$limit;
		$sql .= " limit {$start},{$limit}";
		$find=$mysql->getAll($sql);//搜索后的数据
	}
	//分类的页面进行文章搜索
	// if (!empty($_GET['find'])) {
	// 	$seek=$_GET['seek'];
	// 	if (empty($seek)) {
	// 		header("location:index.php");
	// 	}else{
	// 		$sql = "SELECT count(*) FROM article WHERE title LIKE '%{$_GET['seek']}%' or author LIKE '%{$_GET['seek']}%'";
	// 		$count = $mysql->getOne($sql);
	// 		$limit = 5;
	// 		$p = new MyPage($count,$limit,$page);
	// 		$pageHtml = $p->render();//搜索后的分页按钮

	// 		// $seek=$_GET['seek'];
	// 		$sql = "SELECT * FROM article WHERE title LIKE '%{$_GET['seek']}%' or author LIKE '%{$_GET['seek']}%'";
	// 		$start = ($page-1)*$limit;
	// 		$sql .= " limit {$start},{$limit}";
	// 		$find=$mysql->getAll($sql);//搜索后的数据
	// 	}
	// }
	


?>
<!DOCTYPE html>
<html>
<head>
	<title>文章列表</title>
	<meta charset="utf-8">
	<script type="text/javascript" src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script>
		<!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
	<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

	<!-- 可选的 Bootstrap 主题文件（一般不用引入） -->
	<!-- <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
	 -->
	<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
	<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</head>
<body background="../../public/img/bg4.jpg">
		<div>&nbsp;</div>
		<div>&nbsp;</div>
	<div class="container bg-info">
		<div class="col-md-12">&nbsp;</div>
		<div class="col-md-12">&nbsp;</div>
		<div class="row">
			<?php include '../nav.php'; ?>
			<div class="col-md-12">&nbsp;</div>
			<div class="col-md-12">&nbsp;</div>
		</div>
		
	</div>
	<div class="container bg-info">
		<div class="col-md-12">&nbsp;</div>
		<div class="col-md-12">&nbsp;</div>
		<div class="panel panel-default">
		  <!-- Default panel contents -->
		  <div class="panel-heading" style="padding-left: 20px;font-size: 18px;height: 25px;line-height: 25px;"><span class="glyphicon glyphicon-th-list"></span>  文章列表&nbsp;&nbsp;&nbsp;
		  	<a href="form.php" class="btn btn-success">添加新文章</a>
		  	<div class="col-md-12">&nbsp;</div>
		  </div>
		  	
		  	<div class="col-md-12">&nbsp;</div>
		  		<?php if (isset($_GET['find'])) { ?>
		  			<?php if (empty($find)) { $find[]=array("Sorry!..(｡•ˇ‸ˇ•｡)…没找到您需要的文章。"); ?>
		  	<table class="table">		
						<thead>
								<?php foreach ($find as $key => $value) { ?>
										<?php foreach ($value as $k => $v) { ?>
											<tr><td colspan="5" style="font-size: 30px;text-align: center;height: 200px;line-height: 200px;color:gray;"><?php echo $v; ?></td></tr>
								<?php }} ?>
						</thead>
						<script type="text/javascript">
							$(function(){
								$('#page_btn').hide();
							});
						</script>
			</table>
					<?php	}else{  ?>
			<table class="table">
				<thead>
					<tr>
						<th id="firstcol" class="text-center">ID</th>
						<th class="text-center" >标　题</th>
						<th class="text-center">作  者</th>
						<!-- <th class="text-center">留言管理</th> -->
						<th class="text-center">修　改</th>
						<th class="text-center">删　除</th>
					</tr>
				</thead>
				<tbody>
			<?PHP foreach ($find as $key => $value) { ?>
					<tr>
						<td class="text-center"><?php echo $value['id']; ?></td>
						<td class="text-center"><?php echo $value['title']; ?></td>
						<td class="text-center"><?php echo $value['author']; ?></td>

						<!-- <td class="text-center"><a href="messages.php?id=<?php echo $value['id']?>" class="btn btn-success"><span class="glyphicon glyphicon-edit" aria-hidden="true"></span>留言管理</a></td> -->
						<td class="text-center"><a href="form.php?id=<?php echo $value['id']?>" class="btn btn-success"><span class="glyphicon glyphicon-edit" aria-hidden="true"></span>　编辑</a></td>
						<td class="text-center"><a id="del" href="del.php?id=<?php echo $value['id']?>" class="btn btn-danger" onclick="return cate_delete()"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span>　删除</a></td>
					</tr>
				</tbody>	
					<?php } ?>
			</table>	
		<?php } ?>
		  		<?php }else{ ?>
		  	<table class="table">
			    	<thead class="text-center">
						<tr>
							<th class="text-center">ID</th>
							<th class="text-center">标  题</th>
							<th class="text-center">作  者</th>
							<!-- <th class="text-center">留言管理</th> -->
							<th class="text-center">编  辑</th>
							<th class="text-center">删  除</th>
						</tr>
					</thead>
					<tbody class="text-center">
						<?php foreach ($result as $key => $value): ?>
							<tr>
								<td class="text-center"><?php echo $value['id'] ?></td>
								<td class="text-center"><?php echo $value['title'] ?></td>
								<td class="text-center"><?php echo $value['author'] ?></td>
								<!-- <td class="text-center"><a href="messages.php?id=<?php echo $value['id']?>" class="btn btn-success"><span class="glyphicon glyphicon-edit" aria-hidden="true"></span>留言管理</a></td> -->
								<td class="text-center"><a href="form.php?id=<?php echo $value['id']?>" class="btn btn-success"><span class="glyphicon glyphicon-edit" aria-hidden="true"></span>　编辑</a></td>
								<td class="text-center"><a id="del" href="del.php?id=<?php echo $value['id']?>" class="btn btn-danger" onclick="return cate_delete()"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span>　删除</a></td>
							</tr>
						<?php endforeach ?>
					</tbody>
			</table>
				<?php } ?>
			<div class="col-md-12">&nbsp;</div>
			<div id="page_btn">
				<?php echo $pageHtml; ?>
			</div>
		</div>
		<script type="text/javascript">
			$('#page_btn a').attr('class','btn btn-success');
			$('#page_btn').attr('class','text-center');
			function cate_delete(){
				if (confirm("确认要删除该文章吗？")) {
					return true;
				}else{
					return false;
				}
			}
		</script>
		<div>&nbsp;</div>
	</div>
	<div>&nbsp;</div>
</body>
</html>
